#pragma once

#include "sframe.h"
#include "templatearray.h"

class SAnimation
{
private:
	TemplateArray<SFrame> m_frames;
	/** how much space frames in this animation take */
	SRect m_dimension;
public:
	SAnimation(){}
	void release()
	{
		for(int i = 0; i < m_frames.size(); ++i)
			m_frames.get(i).release();
		m_frames.release();
	}
	/** copy method */
	void copy(const SAnimation & sa)
	{
		release();
		m_frames.copy(sa.m_frames);
		m_dimension.set(m_dimension);
	}
	/** copy constructor */
	SAnimation(const SAnimation & sa){copy(sa);}
	/** must overload the assignment operator to work with custom templated data structs */
	SAnimation & operator=(const SAnimation & sa){copy(sa);return *this;}
	~SAnimation(){release();}
	inline int getFrameCount(){return m_frames.size();}
	inline SFrame * getFrame(const int & a_index){return &m_frames.get(a_index);}
	inline TemplateArray<SFrame> * getFrames(){return &m_frames;}
	inline void invalidateFrameSize()
	{
		for(int i = 0; i < getFrameCount(); ++i)
			getFrame(i)->invalidateFrameSize();
		m_dimension.set(0,0,0,0);
	}
	SRect * getFrameSize()
	{
		if(!m_dimension.isValid() && getFrameCount() > 0)
		{
			m_dimension.set(*getFrame(0)->getFrameSize());
			for(int i = 1; i < getFrameCount(); ++i)
				m_dimension.add(*getFrame(i)->getFrameSize());
		}
		return &m_dimension;
	}
	inline Uint32 getFrameDelay(const int & a_index){return getFrame(a_index)->getDelay();}
	void draw(SDL_Surface * a_screen, const SPoint & a_center, 
		const int & a_index, const int & a_flip, const int & a_zoom) const
	{
		if(a_index < m_frames.size())
			m_frames.get(a_index).draw(a_screen, a_center, a_flip, a_zoom);
	}
};
